/**
 * 
 */
package ca.uwindsor.cs.deepweb.estimation;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * RandomWordSelector could read a dictionary first and build a HashSet to store
 * them. When using this Class, you could randomly pick a word from a
 * dictionary. Every work that the invoker got is identical.
 * 
 * @author Liang Jie
 */
public class RandomWordSelector {

	/**
	 * Instance of buffered reader to read the dictionary
	 */
	protected BufferedReader bufferedreader_in;

	/**
	 * The word that has been got from the dictionary
	 */
	protected Set<Integer> set_index;

	/**
	 * Store All the words in Dictionary
	 */
//	protected HashSet<String> set_words;
	
	protected ArrayList<String> list_words;
	
	/**
	 * The Random object for generating the random number
	 */
	protected Random random;

	/**
	 * Restore used words from the dictionary
	 */
	protected Set<String> set_UsedWord;
	
	/**
	 * Constructor, initialize the objects
	 * 
	 * @param path
	 *            The path of the dictionary
	 * @param encoding
	 *            The encoding of the dictionary
	 * @throws IOException 
	 */
	public RandomWordSelector(String path, String encoding) throws IOException {
		set_index = new HashSet<Integer>();
//		set_words = new HashSet<String>();
		list_words = new ArrayList<String>();
		set_UsedWord = new HashSet<String>();
		random = new Random();
		InputStreamReader read = null;
		try {
			read = new InputStreamReader(new FileInputStream(path), encoding);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(0);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(1);
		}
		bufferedreader_in = new BufferedReader(read);

		getWordfromDictionary();
	}

	/**
	 * Return a random word from the dictionary
	 * 
	 * @return The random word
	 */
	public String getRandomPhrase() {
		int r = random.nextInt(list_words.size());
		String word = new String();
		while (set_index.contains(r)) {
			r = random.nextInt(list_words.size());
		}
		set_index.add(r);
		word = list_words.get(r);
		return word.trim();
	}

	/**
	 * Generate WordsList
	 * @throws IOException 
	 */
	protected void getWordfromDictionary() throws IOException {
			String string_words = new String();
			while ((string_words = bufferedreader_in.readLine()) != null) {
				string_words = string_words.trim();
				if (!string_words.equals("")) {
//					set_words.add(string_words);
					list_words.add(string_words);
				}
			}
			bufferedreader_in.close();
//			list_words.addAll(set_words);
//			set_w
//			set_words.clear();

	}
	
	/**
	 * Return a random word from the dictionary. All are different.
	 * 
	 * @return The random word
	 */
	public String getRandomCharacter() {
		int int_arraylist_words_size = list_words.size();
		int r = random.nextInt(int_arraylist_words_size);
		String string_words = new String();
		string_words = list_words.get(r);

		char[] char_words = string_words.toCharArray();
		r = random.nextInt(char_words.length);
		char char_character = char_words[r];

		while (set_UsedWord.contains(String.valueOf(char_character))) {
			r = random.nextInt(int_arraylist_words_size);
			string_words = list_words.get(r);
			char_words = string_words.toCharArray();
			random.nextInt(char_words.length);
			char_character = char_words[r];
		}
		return String.valueOf(char_character);
	}
	
	/**
	 * Return a random word from the dictionary. All are different.
	 * 
	 * @return The random word
	 */
	public String getRandomWord() {
		int int_arraylist_words_size = list_words.size();
		int r = random.nextInt(int_arraylist_words_size);
		String string_line = new String();
		string_line = list_words.get(r);

		String[] words = string_line.split(" ");
		for(String word: words){
			if(set_UsedWord.contains(word.trim())){
				continue;
			}else{
				return word.trim();
			}
		}
		return getRandomWord();
	}
	
	/**
	 * return the word list that loaded from the file
	 * @return the word list
	 */
	public ArrayList<String> getWordList(){
		return this.list_words;
	}

	/**
	 * Get the size of the Dictionary
	 * 
	 * @return The size of the Dictionary
	 */
	public int getDictionarySize() {
		return list_words.size();
	}
}
